package com.china08.yxyapi.service.continuedu.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.service.continuedu.YxyContinueduLessonService;

@Service
public class YxyContinueduLessonServiceImpl implements YxyContinueduLessonService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public Map<String, Object> findList(String schoolId, Integer lessonType, String keyword, Integer pageNum) {

        List<Object> paramList = new ArrayList<Object>();
        paramList.add(schoolId);
        paramList.add(lessonType);
        StringBuffer sb1 = new StringBuffer();
        sb1.append("  select                                                    ");
        sb1.append("    count(id)                                               ");
        sb1.append("  from yxy_continuedu.yxy_continuedu_lesson                 ");
        sb1.append("  where school_id = ?                                       ");
        sb1.append("    and is_visible = ?                                      ");
        sb1.append("    and status != -1                                        ");
        if (StringUtils.isNotBlank(keyword)) {
            sb1.append("    and name like ?                                     ");
            paramList.add(keyword);
        }
        Integer allSize = jdbcTemplate.queryForObject(sb1.toString(), paramList.toArray(), Integer.class);

        StringBuffer sb2 = new StringBuffer();
        sb2.append("  select                                                    ");
        sb2.append("    id,                                                     ");
        sb2.append("    name,                                                   ");
        sb2.append("    cover,                                                  ");
        sb2.append("    author,                                                 ");
        sb2.append("    created_date,                                           ");
        sb2.append("    is_visible,                                             ");
        sb2.append("    last_modified_date,                                     ");
        sb2.append("    up_date,                                                ");
        sb2.append("    down_date                                               ");
        sb2.append("  from yxy_continuedu.yxy_continuedu_lesson                 ");
        sb2.append("  where school_id = ?                                       ");
        sb2.append("    and is_visible = ?                                      ");
        sb2.append("    and status != - 1                                       ");
        if (StringUtils.isNotBlank(keyword)) {
            sb2.append("    and name like ?                                     ");
        }
        if (lessonType == 1) {
            sb2.append(" order by up_date desc                                  ");
        }
        if (lessonType == 0) {
            sb2.append(" order by created_date desc                             ");
        }
        if (lessonType == 2) {
            sb2.append(" order by down_date desc                                ");
        }
        sb2.append(" limit ?,?                                                  ");
        paramList.add((pageNum - 1) * 10);
        paramList.add(10);
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sb2.toString(), paramList.toArray());

        Map<String, Object> map = new HashMap<>();
        map.put("allSize", allSize);
        map.put("list", list);
        return map;
    }

}
